package cn.bocweb.spinning.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.CompanyList2Bean;

/**
 * Created by chenchangxing on 15/12/18.
 */
public class SearchCompanyResultAdapter extends RecyclerView.Adapter {

    List<CompanyList2Bean.DataBean> list;



    OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        public void onItemClick(String id, String collect);
    }

    public SearchCompanyResultAdapter(List<CompanyList2Bean.DataBean> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_company_result, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder myHolder = (ViewHolder)holder;

        myHolder.company.setText(list.get(position).getIntro_name());
        myHolder.product.setText(list.get(position).getIntro_mainpro());
        myHolder.countProduct.setText(list.get(position).getNum());
        myHolder.address.setText(list.get(position).getAddress());

        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(list.get(position).getId(), list.get(position).getCollect());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.company)
        TextView company;
        @Bind(R.id.product)
        TextView product;
        @Bind(R.id.count_product)
        TextView countProduct;
        @Bind(R.id.address)
        TextView address;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
